package com.wutuo.aggragation.child.controller;

import cn.apestar.agg.common.security.BaseAuthController;
import cn.apestar.core.base.BaseMessage;
import com.wutuo.child.api.ChildAPIService;
import com.wutuo.child.api.request.ChildRequest;
import com.wutuo.child.api.request.ListChildRequest;
import com.wutuo.child.api.response.ChildResponse;
import com.wutuo.child.api.response.RollCallTaskResponse;
import com.wutuo.child.provider.enums.RollCallState;
import com.wutuo.roster.api.RosterAPIService;
import com.wutuo.roster.provider.domain.Roster;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 孩子 <br>
 * 详细说明.
 * <p>
 *
 * @author lvh
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/child", produces = "application/json")
public class ChildController extends BaseAuthController {

    @Autowired
    private ChildAPIService childService;

    @Autowired
    private RosterAPIService rosterService;

    /**
     * 新增孩子
     *
     * @param request
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object create(
            @RequestBody @Valid ChildRequest request) {

        if (request.getUserId() == null)
            request.setUserId(getCurrentUserId());
        Roster roster = rosterService.find(request.getRosterId());
        if (roster != null) {
            request.setInstitutionId(roster.getCmInstitutionId());
            childService.addorUpdate(request);
        }
        return new BaseMessage();
    }

    /**
     * 删除孩子
     *
     * @param userId
     */
    @RequestMapping(value = "/del/parent/{userId}/child/{rosterId}", method = RequestMethod.GET)
    public Object del(@PathVariable("userId") Long userId,
                      @PathVariable("rosterId") Long rosterId) {

        childService.del(userId, rosterId);
        return new BaseMessage();
    }

    /**
     * 查询孩子
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/lisForPage", method = RequestMethod.POST)
    public List<ChildResponse> lisForPage(@RequestBody ListChildRequest request) {

        List list = childService.lisForPage(request);
        return list;
    }

    /**
     * 批量编辑误点人员
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateLate(
            @RequestBody ChildRequest request) {

        request.setState(RollCallState.LATE.getValue());
        String childIn = request.getChildListId();
        if (!StringUtils.isEmpty(childIn)) {
            childIn = childIn.replaceAll("\\|", ",");
            request.setChildListId(childIn);
            childService.updateLate(request);
        }
        return new BaseMessage();
    }

    /**
     * 查询点名状态
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/listRollCallTaskResponsePage", method = RequestMethod.POST)
    public List<RollCallTaskResponse> listRollCallTaskResponsePage(
            @RequestBody ListChildRequest record) {

        return childService.listRollCallTaskResponsePage(record);
    }

}
